public class Solution671 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public int findSecondMinimumValue(TreeNode root) {
        return dfs(root,root.val);
    }

    public int dfs(TreeNode treeNode,int min){
        if (treeNode.left==null){
            return treeNode.val==min?-1:treeNode.val;
        }else{
            int t1=dfs(treeNode.left,min);
            int t2=dfs(treeNode.right,min);
            int temp=-1;
            if (t1>min) {
                temp=t1;
            }
            if (t2>min){
                temp=temp==-1?t2:Math.min(temp,t2);
            }
            return temp;
        }
    }

    public static void main(String[] args) {
        TreeNode treeNode=new TreeNode(2,new TreeNode(2),new TreeNode(5,new TreeNode(5),new TreeNode(7)));
        System.out.println(new Solution671().findSecondMinimumValue(treeNode));
    }
}
